package com.bilibili.juc.cf.one;

import com.bilibili.juc.cf.base.CompletableFutureAdapter;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/*
将 CompletableFuture 转为 ListenableFuture （需适配）
 */
public class GatherCompletableFutureResultsExample3 {
    public static void main(String[] args) throws Exception{
        // 创建多个 CompletableFuture<String> 任务
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Result 1");
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Result 2");
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "Result 3");

        List<CompletableFuture<String>> futures = List.of(future1, future2, future3);
        List<ListenableFuture<String>> listenableFutures = futures.stream().map(CompletableFutureAdapter::adapt).collect(Collectors.toList());


        // 合并所有结果到 List<String>
// 将 CompletableFuture 转为 ListenableFuture（需适配）
        ListenableFuture<List<String>> combinedFuture = Futures.allAsList(listenableFutures);

        // 获取最终结果
        List<String> results = combinedFuture.get();
        System.out.println(results); // 输出: [Result 1, Result 2, Result 3]
    }
}